#include<iostream>
#include<cmath>
#include "GL\glew.h"
#include "GL\GL.h"

using namespace std;

/* -*- Mode: c; c-indentation-style: stroustrup; c-basic-offset: 4 -*- */

#include <GL/glut.h>
#define PI 3.14159265

GLdouble revolutionAngle;
GLdouble rotationAngle;
GLdouble moonRevolutionAngle;
bool hasOrbit = true;
GLfloat light_color[4] = {1.0,1.0,1.0,1.0};
GLfloat sun_color[4] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat cube_spec[4] = { 0.2, 0.2, 0.1, 1.0 };
GLfloat sun_position[4] = {0, 0, 0, 1};
GLfloat moon_color[4] = {0.5,0.5,0.5,1.0};
GLdouble R = 30;
GLdouble r = 4;
int frameCount = 0;
int previousTime = 0;
int fps = 0;

void calculateFPS()
{
	frameCount++;
	int currentTime = glutGet(GLUT_ELAPSED_TIME);
	int timeInterval = currentTime - previousTime;
	if (timeInterval>1000)
	{
		fps = frameCount/(timeInterval/1000.0f);
	}
}

void init() {
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);
	
    glClearColor(0, 0, 0, 0);
    revolutionAngle = 0;
	rotationAngle = 0;
	moonRevolutionAngle = 0;
}


void anim() {
    revolutionAngle += (360.0/4500.0);
	rotationAngle += (360.0/150.0);
	moonRevolutionAngle += (360.0/750.0);
    if (revolutionAngle>360) revolutionAngle -= 360;
	if (rotationAngle>360) 
	{
		rotationAngle -=360;
	}
	if (moonRevolutionAngle>360)
	{
		moonRevolutionAngle -=360;
	}
	
    glutPostRedisplay();
}

void display(void) 
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glShadeModel(GL_SMOOTH);
	glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
	glPushMatrix();
    glLoadIdentity();
	
	
	glMaterialfv(GL_FRONT, GL_EMISSION, sun_color);
	glLightfv(GL_LIGHT0,GL_POSITION,sun_position);
	glLightfv(GL_LIGHT0,GL_DIFFUSE,light_color);
	//glLightfv(GL_LIGHT0,)
    glutSolidSphere(2,50,50);    // draw the sun
	if (hasOrbit)
	{

		glDisable(GL_LIGHTING);
		glBegin(GL_LINE_LOOP);
		for (int angle = 0;angle<360; angle+=1)
		{
			glVertex3f(R/2*cos(double(angle*PI/180)),0,R/2*sin(double(angle*PI/180)));
		}
		glEnd();
		glEnable(GL_LIGHTING);
	}
	glRotated(revolutionAngle,0,1,0);   // rotate the coordinate system
	glTranslated(0,0,R/2);
	
	
	glPushMatrix(); // reserve the state for the earth based coordinate system.
	glRotated(rotationAngle,0,1,0);
	glutSolidSphere(1,10,10);
	glPopMatrix();
	//based on the earth based coordinate system, we rotate it to form the moon based coordinate system.
	glRotated(moonRevolutionAngle,0,1,0); 
	glTranslated(0,0,r/2);
	glPushAttrib(GL_LIGHTING_BIT);
	glMaterialfv(GL_FRONT,GL_DIFFUSE,moon_color);
	glColor4f(0.5,0.5,0.5,1);
	glutSolidSphere(0.5,10,10);
	glPopAttrib();
	// to build the pole line
	glDisable(GL_LIGHTING);
	glPushAttrib(GL_CURRENT_BIT);
	glColor4f(1,1,1,1);
	glLineWidth(2);
	glBegin(GL_LINE_STRIP);
	glDisable(GL_TEXTURE_2D);
	glVertex3f(0,-2,0);
	glVertex3f(0,2,0);	
	glEnd();
	glEnable(GL_LIGHTING);
	glPopAttrib();

	
	

	

	


	
	glPopMatrix();
    glutSwapBuffers();
}

void reshape(int w, int h) {
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
	
    gluPerspective(45, (GLdouble)w/(GLdouble)h, 0.1, 1000);
	gluLookAt(0,30,30,
		0,0,0,
		0,1,0
		);
    glMatrixMode(GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y) {

     switch (key) {
	 case 'o': hasOrbit = !hasOrbit; break;
     case 3:             /* Ctrl-C */
     case 27:            /* ESC */
         exit(0);
     }
}

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(600, 600);
    glutCreateWindow("dummy");
    init();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutIdleFunc(anim);
    glutReshapeFunc(reshape);
    glutMainLoop();
}
